import {
  ArgumentMetadata,
  HttpException,
  HttpStatus,
  Injectable,
  PipeTransform,
} from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';

@Injectable()
export class PpPipe implements PipeTransform {
  async transform(value: any, metadata: ArgumentMetadata) {
    console.log('metadata', metadata, value);
    const TDO = plainToInstance(metadata.metatype, value);
    console.log('TDO', TDO);
    const errs = await validate(TDO);
    console.log('errs', errs);
    if (errs.length > 0) {
      throw new HttpException(errs, HttpStatus.BAD_REQUEST);
    }
    return value;
  }
}
